package wint.mvc.pipeline;

import java.util.Iterator;
import java.util.List;

import wint.lang.misc.Profiler;
import wint.mvc.flow.InnerFlowData;
import wint.mvc.pipeline.valves.Valve;

public class PipelineContext {
	
	private Iterator<Valve> valves;
	
	private boolean broken = false;
	
	public PipelineContext(List<Valve> inputValves) {
		super();
		valves = inputValves.iterator();
	}

	public void invokeNext(InnerFlowData flowData) {
		if (broken) {
			return;
		}
		if (!valves.hasNext()) {
			return;
		}
		Valve valve = valves.next();
		try {
			Profiler.enter("invoke valve " + valve.getName());
			valve.invoke(this, flowData);
		} finally {
			Profiler.release();
		}
	}
	
	public void breakPipeline() {
		broken = true;
	}


}
